package com.qfmy.web.app.tool;

import com.qfmy.model.entity.listenBook.AudioResource;
import com.qfmy.model.entity.novel.Novel;
import com.qfmy.model.enums.AudioType;
import com.qfmy.model.enums.NovelStatus;
import com.qfmy.web.app.mapper.listenBook.AudioResourceMapper;
import com.qfmy.web.app.mapper.novel.NovelMapper;
import com.qfmy.model.vo.listenBook.AudioResourceVo;
import com.qfmy.model.vo.novel.NovelVo;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.tool
 * @date 2025/11/05
 * @description 操作小说的工具
 */@Component
public class NovelTools {

    /**
     * 注入Mapper
     */
    @Autowired
    private NovelMapper novelMapper;

    /**
     * 注入听书mapper
     */
    @Autowired
    private AudioResourceMapper audioResourceMapper;

    /**
     * 查询综合评分高的小说(也叫热门小说)
     */
    @Tool(name = "查询热门的小说")
    public List<NovelVo> getHotNovels() {
        //查询热门的小说
        List< Novel> novels = novelMapper.getHotNovelsPage(1,5);
        //如果没有数据，则返回空列表
        if (novels == null || novels.isEmpty()) {
            return Collections.emptyList();
        }
        //转换对象
        return convertToNovel(novels);
    }

    /**
     * 根据小说的描述和类型查询小说
     */
    @Tool(name = "根据小说的描述或者描述或者字数或者作者查询小说")
    public List<NovelVo> searchNovels(@P("小说的描述或者描述或者字数或者作者")String keyword) {
        //根据关键字查询小说信息
        List< Novel> novels = novelMapper.searchNovels(keyword);
        //如果没有数据，则返回空列表
        if (novels == null || novels.isEmpty()) {
            return Collections.emptyList();
        }
        //转换对象
        return convertToNovel(novels);
    }


    /**
     * 根据小说的名称查询具体的小说信息
     */
    @Tool(name = "根据小说名称查询小说信息")
    public NovelVo getNovelsByName(@P("小说名称")String name) {
        //根据名称查询小说信息
        Novel novel = novelMapper.selectByNovelName( name);
        //如果没有数据，则返回空列表
        if (novel == null) {
            return null;
        }
        //创建一个集合
        List<Novel> list = new ArrayList<>();
        list.add(novel);
        return convertToNovel(list).get(0);
    }


    /**
     * 根据类型查询听书数据'
     * */
    @Tool(name = "根据类型查询听书数据")
    public List<AudioResourceVo> getAudioByType(@P("听书类型名称")String type) {
        //根据类型查询听书数据
        List<AudioResource> list = audioResourceMapper.getAudioByCategoryname(type);
        //判断用户需要的类型是否存在
        if (list == null || list.size() == 0) {
            return Collections.emptyList();
        }
        return convertToAudio(list);
    }

    /**
     * 获取热门的听书信息
     */
    @Tool(name = "获取热门的听书信息")
    public List<AudioResourceVo> getHotAudio() {
        //获取热门的听书信息
        List<AudioResource> list = audioResourceMapper.getHotNewBooks(1,5);
        //如果没有数据，则返回空列表
        if (list == null || list.isEmpty())
        {
            return Collections.emptyList();
        }
        return convertToAudio(list);
    }




    /**
     * 转换实体为VO
     */
    private List<NovelVo> convertToNovel(List<Novel> novels) {
        if (novels == null || novels.isEmpty()) {
            return Collections.emptyList();
        }
        return novels.stream().map(novel -> {
            NovelVo novelVo = new NovelVo();
            BeanUtils.copyProperties(novel, novelVo);
            novelVo.setStatus(NovelStatus.getNameByCode(novel.getStatus()));
            return novelVo;
        }).collect(Collectors.toList());

    }

    /**
     * 转换对象
     * @param audioResources
     * @return
     */
    private List<AudioResourceVo> convertToAudio(List<AudioResource> audioResources) {
        //判断集合是否为空
        if (audioResources != null && !audioResources.isEmpty()) {
            return audioResources.stream().map(audioResource -> {
                AudioResourceVo audioResourceVo = new AudioResourceVo();
                BeanUtils.copyProperties(audioResource, audioResourceVo);
                audioResourceVo.setAudioType(AudioType.getMessageByCode(audioResource.getAudioType()));
                return audioResourceVo;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

}
